import { defineStore } from 'pinia'

export const userStore = defineStore('user', {
  state: () => ({
    token: '',
    userInfo: {
      id: '',
      username: '',
      nickname: '',
      email: '',
      avatar: '',
      role: '',
      createTime: ''
    }
  }),

  getters: {
    //判断是否具备token
    isLogin: (state) => !!state.token,
    getUserInfo: (state) => state.userInfo
  },

  actions: {
    setToken(token) {
      this.token = token
    },
    setUserInfo(userInfo) {
      this.userInfo = userInfo
    },
    // 登出操作
    logout() {
      this.token = ''
      this.userInfo = {
        id: '',
        username: '',
        nickname: '',
        email: '',
        avatar: '',
        role: '',
        createTime: ''
      }
    }
  },
  persist: {
    key: 'user',
    paths: ['token', 'userInfo'],
    
  }
})
